#!/bin/bash

# 在CentOS上安装和配置MySQL的脚本

# 1. 安装MySQL服务器
echo "安装MySQL服务器..."
sudo yum update -y
sudo yum install -y mysql-server

# 2. 启动MySQL服务
echo "启动MySQL服务..."
sudo systemctl start mysqld
sudo systemctl enable mysqld

# 3. 获取临时root密码
echo "获取临时root密码..."
temp_password=$(sudo grep 'temporary password' /var/log/mysqld.log | awk '{print $NF}')
echo "临时root密码: $temp_password"

# 4. 运行MySQL安全配置
echo "运行MySQL安全配置..."
sudo mysql_secure_installation

# 5. 登录MySQL并创建数据库和用户
echo "创建数据库和用户..."
mysql -u root -p <<MYSQL_SCRIPT
CREATE DATABASE svt_backend CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'svt_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON svt_backend.* TO 'svt_user'@'localhost';
FLUSH PRIVILEGES;
EXIT
MYSQL_SCRIPT

echo "MySQL设置完成!"
echo "请在app.py中配置数据库连接信息:"
echo "app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://svt_user:your_password@localhost/svt_backend'"